package com.yuven.appframework.util

import android.text.TextUtils
import java.lang.StringBuilder

/**
 * 
 *
 * @author :        Yuven
 * @date :          2022/9/22 10:30
 * @desc :
 */
object PhoneFormatter {

    /**
     * xxx xxxx xxxx格式
     */
    fun format344(src:String?,separator: String):String{
        if (!TextUtils.isEmpty(src)) {
            var sb = StringBuilder(src)
            if(src!!.length==11){
                sb.insert(3,separator)
                sb.insert(8,separator)
            }
            return sb.toString()
        }
        return "";
    }

    fun format(src:String,separator: String):String{
        var result = StringBuilder(replaceFormat(src,separator));
        for (i in src.indices) {
            // 344格式 第 4，9 的位置插入分隔符
            if (i == 3 || i == 8) {
                result.insert(i,separator)
            }
        }
        return result.toString();
    }

    fun getSuffix(src:String, count:Int):String{
        if(src.length>=count){
            val start = src.length - count;
            val end = src.length;
            var src:String = src.substring(start, end);
            return src
        }
        return src
    }

    fun replaceFormat(src:String,separator: String):String{
        return src.replace(separator,"");
    }
}